package com.test.mchelepesa.activity_fragment_rice.base_rice;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.webkit.WebView;

import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.test.mchelepesa.util_rice.event_points.TrackManager_rice;
import com.test.mchelepesa.util_rice.helper_rice.DeviceHelper_rice;
import com.test.mchelepesa.util_rice.helper_rice.UserInfoHelper_rice;

import org.jetbrains.annotations.NotNull;


public class BaseApplication_rice extends Application {

    private String msg;

    @NotNull
    public static BaseApplication_rice gApplication_rice() {
        return baseApplication_rice;
    }

    /**
     * 适配android 9 网络进程
     */
    private void initPieWebView_rice() {
        String processName = gProcessName_rice(this);
        if (!DeviceHelper_rice.getPackageName().equals(processName)) {
            WebView.setDataDirectorySuffix(processName);
        }
    }

    private String gProcessName_rice(Context context) {
        if (context == null) return null;
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager != null) {
            for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
                if (processInfo.pid == android.os.Process.myPid()) {
                    return processInfo.processName;
                }
            }
        }
        return "";
    }

    private static BaseApplication_rice baseApplication_rice;

    @Override
    public void onCreate() {
        super.onCreate();
        baseApplication_rice = this;
        try {

            TrackManager_rice.INSTANCE.init();

            //pie webview 适配
            initPieWebView_rice();

            //firebase
//            FirebaseHelper_rice.getInstance().initGa4EventInfo();

            //gaid
//            GaidHelper.getInstance().initGaidInThread();
//            GaidHelper.getInstance().startGaidHandler();

            //install referrer
//            GPInstallHelper.getInstance().init(this);
//            GPInstallHelper.getInstance().connectToPlayStore();

            //pie webview 适配
            initPieWebView_rice();

            //user info
            UserInfoHelper_rice.getInstance().init(getApplicationContext());

            //syc data sdk
            initSycDataSDK_rice();

        } catch (Exception e) {
            StringUtil_rice.printException(e);
        }
    }

    public void initSycDataSDK_rice() {}


    public int getVersionCode(){

        try {
            return getPackageManager().getPackageInfo(getPackageName(),0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            StringUtil_rice.printException(e);
        }
        return 0;
    }

    public String getVersionCodeStr(){

        try {
            return String.valueOf(getPackageManager().getPackageInfo(getPackageName(),0).versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            StringUtil_rice.printException(e);
        }
        return "0";
    }

    public String getVersionName(){
        try {
            return getPackageManager().getPackageInfo(getPackageName(),0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            StringUtil_rice.printException(e);
        }
        return "";
    }

}
